local guibi_active = fk.CreateSkill{
  name = "hanqing__guibi&",
}

Fk:loadTranslationTable{
  ["hanqing__guibi&"] = "归璧",
  [":hanqing__guibi&"] = "每局游戏限一次，出牌阶段，你可以展示蔺相如至多其装备区牌数张牌，然后其选择一项：1.使用其中的基本牌"..
  "（无距离限制）；2.重铸其中的非基本牌。然后，你获得其余展示牌。",

  ["#hanqing__guibi&"] = "归璧：展示蔺相如的牌，其使用其中的基本牌或重铸其中的非基本牌，你获得其余牌",
  ["#hanqing__guibi-show"] = "归璧：展示 %dest 的至多%arg张牌",
  ["#hanqing__guibi1-use"] = "归璧：使用其中的基本牌（无距离限制），或点“取消”重铸其中的非基本牌。%dest 获得其余的牌",
  ["#hanqing__guibi2-use"] = "归璧：使用其中的基本牌（无距离限制），%dest 获得其余的牌",
}

guibi_active:addEffect("active", {
  mute = true,
  prompt = "#hanqing__guibi&",
  card_num = 0,
  target_num = 1,
  can_use = function (self, player)
    return table.find(Fk:currentRoom().alive_players, function (p)
      return p ~= player and p:hasSkill("hanqing__guibi") and
        not table.contains(p:getTableMark("hanqing__guibi"), player.id) and
        #p:getCardIds("e") > 0
    end)
  end,
  card_filter = Util.FalseFunc,
  target_filter = function (self, player, to_select, selected)
    return #selected == 0 and to_select:hasSkill("hanqing__guibi") and
      not table.contains(to_select:getTableMark("hanqing__guibi"), player.id) and
      #to_select:getCardIds("e") > 0
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local target = effect.tos[1]
    room:addTableMark(target, "hanqing__guibi", player.id)
    target:broadcastSkillInvoke("hanqing__guibi")
    room:notifySkillInvoked(target, "hanqing__guibi", "support")
    local cards = room:askToChooseCards(player, {
      target = target,
      min = 1,
      max = #target:getCardIds("e"),
      flag = "he",
      skill_name = "hanqing__guibi",
      prompt = "#hanqing__guibi-show::"..target.id..":"..#target:getCardIds("e"),
    })
    target:showCards(cards)
    if player.dead or target.dead then return end
    local yes = true
    while not target.dead do
      cards = table.filter(cards, function (id)
        return table.contains(target:getCardIds("he"), id)
      end)
      if #cards == 0 then break end
      local basic, non_basic = {}, {}
      for _, id in ipairs(cards) do
        if Fk:getCardById(id).type == Card.TypeBasic or Fk:getCardById(id).name:endsWith("hanqing__hejiang") then
          table.insert(basic, id)
        else
          table.insert(non_basic, id)
        end
      end
      if #basic > 0 then
        local prompt = "#hanqing__guibi2-use::"..player.id
        if yes then
          prompt = "#hanqing__guibi1-use::"..player.id
        end
        local recorder = {}
        for _, id in ipairs(basic) do
          recorder[id] = Fk:getCardById(id, true):getMark("hanqing__hejiang")
          room:setCardMark(Fk:getCardById(id, true), "hanqing__hejiang", 0)
          room:filterCard(id, target)
        end
        local use = room:askToUseRealCard(target, {
          pattern = basic,
          skill_name = "hanqing__guibi",
          prompt = prompt,
          extra_data = {
            bypass_distances = true,
            bypass_times = true,
            extraUse = true,
          },
          expand_pile = table.filter(basic, function (id)
            return not table.contains(target:getCardIds("h"), id)
          end),
          skip = true,
        })
        for _, id in ipairs(basic) do
          if not (use and use.card.id == id) then
            room:setCardMark(Fk:getCardById(id, true), "hanqing__hejiang", recorder[id])
          end
          room:filterCard(id, target)
        end
        if use then
          yes = false
          if recorder[use.card.id] then
            use.extra_data = use.extra_data or {}
            use.extra_data.hanqing__guibi = {use.card.id, recorder[use.card.id]}
          end
          room:useCard(use)
        else
          break
        end
      end
      if yes then
        non_basic = table.filter(cards, function (id)
          return table.contains(non_basic, id) and table.contains(target:getCardIds("he"), id)
        end)
        room:recastCard(non_basic, target, "hanqing__guibi")
        break
      end
    end
    if not player.dead then
      cards = table.filter(cards, function (id)
        return table.contains(target:getCardIds("he"), id)
      end)
      if #cards > 0 then
        room:moveCardTo(cards, Card.PlayerHand, player, fk.ReasonPrey, "hanqing__guibi", nil, true, player)
      end
    end
  end,
})

return guibi_active
